var app = getApp();
var util = require('../../utils/util.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var Pagination = require('../../utils/pagination.js');
var ajax = require('../../utils/ajax.js');

Page(common.extendPage(PageBase, {
  data: {
    dataLoaded: false,
    goodsList: []
  },

  onLoad(options) {
    // common.initApp({
    //   extSuc: () => {
    //     this._getList();
    //   },
    //   loginSuc: () => {
    //     this.setData({
    //       logoImg: app.getLogoImg()
    //     });
    //     this.$loginSuc();
    //   },
    //   sysInfoSuc: (sysInfo) => {
    //     this.setData({
    //       winHei: app.getWinHei(),
    //     });
    //   },
    // });
    var _self = this;
    var sid = options.id || 0;
    _self._getList(sid);
  },

  onPullDownRefresh() {
    this._pagination && this._pagination.flush(() => {
      wx.stopPullDownRefresh();
    });
  },

  onReachBottom() {
    console.log("开始");
    this._pagination && this._pagination.loadNext();
  },

	/**
	 * 获取商品列表
	 * @return {[type]} [description]
	 */
  _getList(sid) {
    this._pagination = new Pagination({
      page: this,
      scope: 'pagination'
    }).init({
      rendData: (page, cb) => {

        ajax.G({
          url: 'm=goods',
          data: {
            page,
            size: 20,
            sid,
            seckillstatus: 1
          },
          success: (res) => {
            var goodsList = res.data.item;
            //var goodsList = this.data.goodsList || [];
            // var {
            //   goodsList = []
            //           } = res.data;
            var allList = this.data.goodsList || [];
            //debugger;
            this.setData({
              dataLoaded: true,
              goodsList: page == 1 ? goodsList : allList.concat(goodsList)
            });

            cb && cb(goodsList.length);
          }
        });

      }
    });

  }

}));